<template>
  <div class="dashboard-container">
    <div class="app-container">
      <pageTools :show-before="true">
        <span slot="before">共{{ total }}条记录</span>
        <template #after>
          <el-button size="small" type="warning" @click="$router.push('/import')">导入</el-button>
          <el-button size="small" type="danger" @click="exportExcel">导出</el-button>
          <el-button icon="plus" type="primary" size="small" @click="showDialog = true">新增员工</el-button>
        </template>
      </pageTools>

      <el-card>
        <el-table v-loading="loading" border :data="list">
          <el-table-column label="序号" type="index" />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="员工照片" style="width:160">
            <template #default="{row}">
              <img
                v-imageError="require('@/assets/common/head.jpg')"
                :src="row.staffPhoto"
                class="peopleImg"
                @click="showQrcode(row.staffPhoto)"
              >
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <!-- 列表复用数据,用formatter -->
          <el-table-column label="聘用形式" prop="formOfEmployment" :formatter="formOfEmployment" />
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" prop="timeOfEntry">
            <template #default="{row}">
              {{ row.timeOfEntry | formatDate }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" prop="enableState">
            <template v-slot="{row}">
              <el-switch
                :value="+row.enableState"
                :active-value="1"
                :inactive-value="0"
                active-color="#13ce66"
                inactive-color="#ccc"
              />
            </template>
          </el-table-column>
          <el-table-column label="操作" fixed="right" width="280">
            <template #default="{row}">
              <el-button type="text" size="small" @click="$router.push('/employees/detail/'+row.id)">查看</el-button>
              <el-button type="text" size="small">转正</el-button>
              <el-button type="text" size="small">调岗</el-button>
              <el-button type="text" size="small">离职</el-button>
              <el-button type="text" size="small" @click="assignRole(row.id)">角色</el-button>
              <el-button type="text" size="small" @click="del(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <el-row type="flex" justify="center" align="middle" style="height: 60px">
          <el-pagination
            :current-page.sync="page.page"
            :page-sizes="[5,10,20]"
            :page-size.sync="page.size"
            layout="total, sizes, prev, pager, next"
            :total="total"
            @size-change="changeSize"
            @current-change="changeCurrent"
          />
        </el-row>
      </el-card>

      <addEmployeeVue :show-dialog.sync="showDialog" />

      <el-dialog title="请扫描二维码" :visible.sync="showQrcodeDialog">
        <el-row type="flex" justify="center">
          <canvas id="canvas" ref="canvas" />
        </el-row>
      </el-dialog>

      <assignRole ref="assignRole" :show-role-dialog.sync="showRoleDialog" :user-id="userId" />
    </div>
  </div>
</template>

<script>
import QRcode from 'qrcode'
import { getEmployeeList, delEmployee } from '@/api/employees'
import employeeEnum from '@/api/constant/employees'
import addEmployeeVue from './components/add-employee.vue'
import assignRole from './components/assign-role.vue'
export default {
  components: {
    addEmployeeVue, assignRole
  },
  data() {
    return {
      showDialog: false,
      loading: false,
      list: [], // 接数据的
      page: {
        page: 1, // 当前页码
        size: 10
      },
      total: 0, // 总数
      hireType: employeeEnum.hireType,
      showQrcodeDialog: false,
      showRoleDialog: false,
      userId: null
    }
  },

  created() {
    this.getEmployeeList()
  },

  methods: {
    async getEmployeeList() {
      this.loading = true
      const { rows, total } = await getEmployeeList(this.page)
      this.total = total
      this.list = rows
      this.loading = false
    },
    changeSize() {
      this.page.page = 1
      this.getEmployeeList()
    },
    changeCurrent() {
      this.getEmployeeList()
    },
    formOfEmployment(row) {
      // row是每一条数据
      const obj = this.hireType.find(item => item.id === +row.formOfEmployment)
      return obj?.value ? obj?.value : '未知' // 列表展示return的值
    },
    async del(id) {
      try {
        await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning' })
        await delEmployee(id)
        this.getEmployeeList()
        this.$message.success('删除成功')
      } catch (error) {
        this.$message.error(error?.message ? error.message : '取消删除')
      }
    },
    exportExcel() {
      window.location.href = 'http://demo.open.renren.io/renren-security-server/sys/log/login/export?token=3bcc4a4ba8e8b3a6fecb60473a68f5df&creatorName=&status=0'
    },
    showQrcode(url) {
      if (url) {
        this.showQrcodeDialog = true
        this.$nextTick(() => {
          QRcode.toCanvas(this.$refs.canvas, url)
        })
      } else {
        this.$message.error('用户还未上传照片')
      }
    },
    assignRole(id) {
      this.userId = id
      this.showRoleDialog = true
      this.$refs.assignRole.getUserDetailById(id)
    }
  }

}
</script>

<style>
.peopleImg{
    border-radius: 50%;
    width: 100px;
    height: 100px;
    padding: 10px;
}
</style>
